﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Inference
{
    public partial class frmMain : Form
    {
        TreeNode previousSelectedNode = null;

        string GetRoot(int FrameID)
        {
            Frame fr;
            do
            {
                fr = tale.Frame.Where(f => f.ID == FrameID).First();
                if (fr.Is_a.HasValue)
                    FrameID = fr.Is_a.Value;
                else
                    FrameID = 0;
            }
            while (FrameID != 0);
            return fr.Name;
        }
        public void tvLoad()
        {
            ShowWaitingMsg("Пожалуйста, подождите. Идет загрузка базы знаний.");
            var frames = from f in tale.Frame
                         orderby f.Name
                         select f;
            foreach (var f in frames)
            {
                if (GetRoot(f.ID).ToLower() == "ситуация")
                    tvMain.Nodes[0].Nodes[0].Nodes.Add(CreateFrameNode(f));
                else
                    tvMain.Nodes[0].Nodes[1].Nodes.Add(CreateFrameNode(f));
            }

            var domains = from d in tale.Domains
                          orderby d.Name
                          select d;
            foreach (var d in domains)
            {
                tvMain.Nodes[1].Nodes.Add(CreateDomainNode(d));
            }
            HideWaitingMsg();
        }

        TreeNode CreateFrameNode(Frame f)
        {
            TreeNode tn = new TreeNode(f.Name);

            string sISA = "";
            var isa = tale.Frame.Where(fr => fr.ID == f.Is_a);
            if (isa.Count() == 0)
                sISA = "NULL";
            else
                sISA = isa.First().Name;
            tn.Nodes.Add(new TreeNode("IS_A: " + sISA));

            var slots = from s in f.Slot
                        orderby s.Number
                        select s;
            foreach (var s in slots)
            {
                tn.Nodes.Add(CreateSlotNode(s));
            }
            return tn;
        }

        TreeNode CreateSlotNode(Slot s)
        {
            TreeNode tn = new TreeNode(s.Name);

            tn.Nodes.Add(new TreeNode("Домен: " + s.Domains.Name));

            var dvalues = tale.DValues.Where(dv => dv.ID == s.DefaultValue);
            string sDefaultValue = "";
            if (dvalues.Count() == 0)
                sDefaultValue = "NULL";
            else
                sDefaultValue = dvalues.First().Value;
            tn.Nodes.Add(new TreeNode("Задание отсутствия: " + sDefaultValue));

            tn.Nodes.Add(new TreeNode("Вопрос пользователю: " + s.Question));

            string sAllowNull = "";
            if (s.AllowNull)
                sAllowNull = "Да";
            else
                sAllowNull = "Нет";
            tn.Nodes.Add(new TreeNode("Допустимо \"Не знаю\": " + sAllowNull));

            string sInheritance = "";
            switch (s.Inheritance)
            {
                case 1:
                    sInheritance = "Override";
                    break;
                case 2:
                    sInheritance = "Same";
                    break;
                default:
                    sInheritance = "Error";
                    break;
            }
            tn.Nodes.Add(new TreeNode("Тип наследования: " + sInheritance));

            return tn;
        }

        TreeNode CreateDomainNode(Domains d)
        {
            TreeNode tn = new TreeNode(d.Name);
            foreach (var dv in d.DomainsValues)
            {
                tn.Nodes.Add(new TreeNode(dv.DValues.Value));
            }
            return tn;
        }

        private void HighlightTreeView(TreeNode tn)
        {
            foreach (TreeViewHighlighter tvh in settings.TreeViewHighlighters)
            {
                if (tvh.Enabled)
                    tvh.Apply(tn);
            }
        }
        private void UnhighlightTreeView(TreeNode tn)
        {
            foreach (TreeViewHighlighter tvh in settings.TreeViewHighlighters)
            {
                tvh.Clear(tn);
            }
        }

        private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (previousSelectedNode != e.Node)
            {
                previousSelectedNode = e.Node;
                HighlightTreeView(e.Node);
            }
        }

        private void tvMain_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (previousSelectedNode != null
                &&
                previousSelectedNode != e.Node)
                UnhighlightTreeView(previousSelectedNode);
        }
       
    }
}
